
package com.koron.util.dbconfig;

import com.koron.css2.ApplicationConfig;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.mybatis.spring.annotation.MapperScan;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.koron.css2.newProjectCenter.function.oracleMapper", sqlSessionFactoryRef = "oracleSqlSessionFactory")
public class OracleDataSourceConfig {

    @Bean(name = "oracleDataSource")
    public DataSource oracleDataSource() {
        HikariConfig config = new HikariConfig();
        config.setDriverClassName("oracle.jdbc.OracleDriver");
        config.setJdbcUrl(ApplicationConfig.getOracleUrl());
        config.setUsername(ApplicationConfig.getOracleUsername());
        config.setPassword(ApplicationConfig.getOraclePassword());
        config.setMaximumPoolSize(10); // 最大连接数
        config.setMinimumIdle(2);     // 最小空闲连接数
        config.setIdleTimeout(30000); // 空闲超时时间（毫秒）

        // 添加解决字符集问题的参数
        config.addDataSourceProperty("oracle.jdbc.defaultNChar", "true"); // 开启 NChar 支持
        config.addDataSourceProperty("charset", "ZHS16GBK");             // 设置字符集为 ZHS16GBK
        config.addDataSourceProperty("defaultRowPrefetch", "20");        // 优化查询行预取数量

        return new HikariDataSource(config);
    }

    @Bean(name = "oracleSqlSessionFactory")
    public SqlSessionFactory oracleSqlSessionFactory(DataSource oracleDataSource) throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(oracleDataSource);
        return sessionFactory.getObject();
    }
}